package cn.iocoder.yudao.module.vos.controller.admin.activitytask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.activitytask.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.activitytask.ActivityTaskDO;
import cn.iocoder.yudao.module.vos.service.activitytask.ActivityTaskService;

@Tag(name = "管理后台 - 活动任务详情")
@RestController
@RequestMapping("/vos/activity-task")
@Validated
public class ActivityTaskController {

    @Resource
    private ActivityTaskService activityTaskService;

    @PostMapping("/create")
    @Operation(summary = "创建活动任务详情")
    @PreAuthorize("@ss.hasPermission('vos:activity-task:create')")
    public CommonResult<Long> createActivityTask(@Valid @RequestBody ActivityTaskSaveReqVO createReqVO) {
        return success(activityTaskService.createActivityTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动任务详情")
    @PreAuthorize("@ss.hasPermission('vos:activity-task:update')")
    public CommonResult<Boolean> updateActivityTask(@Valid @RequestBody ActivityTaskSaveReqVO updateReqVO) {
        activityTaskService.updateActivityTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动任务详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:activity-task:delete')")
    public CommonResult<Boolean> deleteActivityTask(@RequestParam("id") Long id) {
        activityTaskService.deleteActivityTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除活动任务详情")
                @PreAuthorize("@ss.hasPermission('vos:activity-task:delete')")
    public CommonResult<Boolean> deleteActivityTaskList(@RequestParam("ids") List<Long> ids) {
        activityTaskService.deleteActivityTaskListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动任务详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:activity-task:query')")
    public CommonResult<ActivityTaskRespVO> getActivityTask(@RequestParam("id") Long id) {
        ActivityTaskDO activityTask = activityTaskService.getActivityTask(id);
        return success(BeanUtils.toBean(activityTask, ActivityTaskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动任务详情分页")
    @PreAuthorize("@ss.hasPermission('vos:activity-task:query')")
    public CommonResult<PageResult<ActivityTaskRespVO>> getActivityTaskPage(@Valid ActivityTaskPageReqVO pageReqVO) {
        PageResult<ActivityTaskDO> pageResult = activityTaskService.getActivityTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动任务详情 Excel")
    @PreAuthorize("@ss.hasPermission('vos:activity-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportActivityTaskExcel(@Valid ActivityTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityTaskDO> list = activityTaskService.getActivityTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "活动任务详情.xls", "数据", ActivityTaskRespVO.class,
                        BeanUtils.toBean(list, ActivityTaskRespVO.class));
    }

}